/*
 * @LastEditTime: 2023-02-28 14:36:34
 */
import { router } from "../index";
import { NewAlbum } from "../../Models/NewAlbum.schema";
import { Album } from "../../Models/Album.schema";
import { AlbumController } from "../../Controller/Album.controller";
import { createAlbumDTO, updateAlbumDTO } from "../../DTO/Album.dto";
import { NewAlbumController } from "../../Controller/NewAlbum.controller";

const AlbumModel = new AlbumController(Album);
const newAlbumModel = new NewAlbumController(NewAlbum);

//搜索专辑
router.get("/album", async (ctx, next) => {
  // 标题查询
  if (ctx.request.query.title) {
    console.log(111)
    console.log(ctx.request.query.title)
    const res = await AlbumModel.findByTitle(ctx.request.query.title.toString()) as { code: number, message: string };
    res.code === 400 ? ctx.status = 400 : null;
    ctx.body = res;
  }
  // ID 查询
  else if (ctx.request.query.id) {
    ctx.body = await AlbumModel.findById(ctx.request.query.id.toString());
  }
  // 随机查询
  else if (ctx.request.query.random) {
    ctx.body = await AlbumModel.findBySize(Number(ctx.request.query.random));
  }
});
//获取专辑详情
router.get("/album/info", async (ctx, next) => {
  if (ctx.request.query.id) {
    ctx.body = await AlbumModel.getInfo(ctx.request.query.id.toString())
  }
  else {
    ctx.body = {
      message: '输入错误,请通过id查询具体信息'
    }
  }
});
// 创建专辑
router.post('/album', async (ctx, next) => {
  console.log(ctx.request.body);
  ctx.body = await AlbumModel.createItem(ctx.request.body as createAlbumDTO);
});

//删除专辑
router.delete("/album/:id", async (ctx, next) => {
  ctx.body = await AlbumModel.removeById(ctx.params.id);
});
// 更新专辑
router.patch("/album/:id", async (ctx, next) => {
  if (ctx.params.id)
    ctx.body = await AlbumModel.updateById(
      ctx.params.id,
      ctx.request.body as updateAlbumDTO
    );
});
// 获取本周新碟
router.get("/album/top", async (ctx, next) => {
  console.log("第一层")
  ctx.body = await newAlbumModel.getNew();
});
//获取全部专辑
router.get("/album/:all", async (ctx, next) => {
  console.log(222)
  ctx.body = await AlbumModel.findAll();
});
